home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / lib / panelset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-22  |  3.5 KB  |  152 lines

  1. /*    PANELSET . C
  2. #
  3. %    Copyright (c)    Jin Guojun
  4. %
  5. %    Initial and Set Colors for Panel Parts
  6. %
  7. % AUTHOR:    Jin Guojun - LBL    12/12/91
  8. */
  9.  
  10. #include "panel.h"
  11.  
  12. int    precision=384, VCTEntry;/* for default cmap    */
  13. XColor    qcolor[MaxColors];    /* for public use.    */
  14. Colormap    lastmap;
  15.  
  16. void
  17. set_button_color(b, bc, pc, tc)
  18. Button    *b;
  19. {
  20. register int    i;
  21. if (bc >= 0)
  22.     for (i=b->numb, b->vcolor=bc; i--;)
  23.     b->color[i] = bc;
  24. if (pc>=0)
  25.     b->dimcolor = pc;
  26. if (tc>=0)
  27.     b->tcolor = tc;
  28. }
  29.  
  30. void    set_pressbutton_color(p, bc, pc, tc)
  31. PressButton    *p;
  32. {
  33. if (bc>=0)
  34.     p->color.v = bc;
  35. if (pc>=0)
  36.     p->color.p = pc;
  37. if (tc>=0)
  38.     p->tcolor = tc;
  39. }
  40.  
  41. void    set_slider_color(s, rc, bc, tc)
  42. Slider    *s;
  43. {
  44. if (rc>=0)
  45.     s->rcolor = rc;
  46. if (bc>=0) {
  47.     register int    i = s->numb;
  48.     while (i--)
  49.         if (bc < 128)
  50.         s->scolor[i] = bc + (s->numb-i << 3);
  51.         else
  52.         s->scolor[i] = bc - (s->numb-i << 3);
  53.     }
  54. if (tc>=0)
  55.     s->tcolor = tc;
  56. }
  57.  
  58. void
  59. Set_Panel(ip, item, op1, op2, op3)
  60. VType    *ip;
  61. {
  62. switch (item) {
  63. case BUTTON_COLOR:    /* button color, pressed color, title color */
  64.     set_button_color(ip, op1, op2, op3);
  65.     break;
  66. case PRESS_BUTTON_COLOR:
  67.     set_pressbutton_color(ip, op1, op2, op3);
  68.     break;
  69. case SLIDER_COLOR:    /* rail, bar1, bar2 */
  70.     set_slider_color(ip, op1, op2, op3);
  71.     break;
  72. default:    mesg("invalid panel set\n");
  73. }
  74. }
  75.  
  76. /*===============================================
  77. %    return the 1st available entry in cmap    %
  78. %    if no entry left, then return 0. system    %
  79. %    colormap must use at lease the first    %
  80. %    a couple of entries for blcak and white    %
  81. ===============================================*/
  82. PColor
  83. GetVctEntry(dpy, src, cmap, set_gray)
  84. Display*    dpy;
  85. Colormap    cmap;
  86. {
  87. XColor    ccell;
  88.  
  89. /*    set standard BLACK, WHITE, and GRAYs    */
  90. Black = XBlackPixel(dpy, src);
  91. White = XWhitePixel(dpy, src);
  92.  
  93. ccell.flags = DoAll;
  94. ccell.pixel = 0;
  95. if (XAllocColorCells(dpy, cmap, False, 0, 0, &ccell.pixel, 1))    {
  96.     DEBUGMESSAGE("[%d]entry = %u\n", cmap, ccell.pixel);
  97.     XFreeColors(dpy, cmap, &ccell.pixel, 1, 0);
  98.     if (set_gray && ccell.pixel<32) {
  99.         ccell.red = ccell.green = ccell.blue = 80<<8;
  100.         XAllocColor(dpy, cmap, &ccell);    darkGray = ccell.pixel;
  101.         ccell.red = ccell.green = ccell.blue = 128<<8;
  102.         XAllocColor(dpy, cmap, &ccell);    Visible = ccell.pixel;
  103.         ccell.red = ccell.green = ccell.blue = 168<<8;
  104.         XAllocColor(dpy, cmap, &ccell);    lightGray = ccell.pixel;
  105.         ccell.red = ccell.green = ccell.blue = 200<<8;
  106.         XAllocColor(dpy, cmap, &ccell);    Gray = ccell.pixel++;
  107.     }
  108. }
  109. else    message("[%d] no entry available\n", cmap);
  110. return    VCTEntry=ccell.pixel;
  111. }
  112.  
  113. /*======================================================
  114. %    Find the the closest color in the colormap.
  115. %    Less Pseudo value get more close color but
  116. %    easy to fail. The better value is 256.
  117. ======================================================*/
  118. PColor    GetCloseColor(dpy, cmap, ncolors, uct, r, g, b)
  119. Display    *dpy;
  120. Colormap    cmap;
  121. XColor    *uct;
  122. register int    r, g, b;
  123. {
  124. register int    dis, i=MaxColors;
  125. register XColor    *cp = uct;    /* user color table */
  126. PColor    value=-1, Pseudo=precision;
  127.  
  128. if (! qcolor[i>>1].pixel)    /* initial    */
  129.     while (i--)
  130.         qcolor[i].pixel = (u_long) i;
  131. if (dpy && cmap==DefaultColormap(dpy, XDefaultScreen(dpy))
  132.     && ncolors+VCTEntry<256)
  133.     ncolors += VCTEntry;
  134. if (!uct || cmap) {
  135.     cp = qcolor;
  136.     if (lastmap != cmap)    /* system color-map */
  137.         XQueryColors(dpy, lastmap=cmap, cp, ncolors);
  138. }
  139. for (i=0; i < ncolors; i++) {
  140.     dis = abs((cp[i].red >> 8) - r) +
  141.         abs((cp[i].green >> 8) - g) +
  142.         abs((cp[i].blue >> 8) - b);
  143.     if (!dis)
  144.         return (cp[i].pixel);    /* find exact value    */
  145.     else if (dis < Pseudo) {
  146.         Pseudo = dis;
  147.         value = cp[i].pixel;    /* get the closest one    */
  148.     }
  149. }
  150. return    value;
  151. }
  152.